package cn.iocoder.yudao.module.museum.controller.admin.collectionmanagement;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.museum.controller.admin.collectionmanagement.vo.*;
import cn.iocoder.yudao.module.museum.dal.dataobject.collectionmanagement.CollectionManagementDO;
import cn.iocoder.yudao.module.museum.service.collectionmanagement.CollectionManagementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 藏品管理")
@RestController
@RequestMapping("/museum/collection-management")
@Validated
public class CollectionManagementController {

    @Resource
    private CollectionManagementService collectionManagementService;

    @PostMapping("/create")
    @Operation(summary = "创建藏品管理")
    @PreAuthorize("@ss.hasPermission('museum:collection-management:create')")
    public CommonResult<CollectionManagementRespVO> createCollectionManagement(@Valid @RequestBody CollectionManagementSaveReqVO createReqVO) {
        return success(collectionManagementService.createCollectionManagement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新藏品管理")
    @PreAuthorize("@ss.hasPermission('museum:collection-management:update')")
    public CommonResult<CollectionManagementRespVO> updateCollectionManagement(@Valid @RequestBody CollectionManagementSaveReqVO updateReqVO) {
        return success(collectionManagementService.updateCollectionManagement(updateReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除藏品管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('museum:collection-management:delete')")
    public CommonResult<Boolean> deleteCollectionManagement(@RequestParam("id") Long id) {
        collectionManagementService.deleteCollectionManagement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得藏品管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('museum:collection-management:query')")
    public CommonResult<CollectionManagementRespVO> getCollectionManagement(@RequestParam("id") Long id) {
        CollectionManagementDO collectionManagement = collectionManagementService.getCollectionManagement(id);
        return success(BeanUtils.toBean(collectionManagement, CollectionManagementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得藏品管理分页")
    @PreAuthorize("@ss.hasPermission('museum:collection-management:query')")
    public CommonResult<PageResult<CollectionManagementRespVO>> getCollectionManagementPage(@Valid CollectionManagementPageReqVO pageReqVO) {
        PageResult<CollectionManagementDO> pageResult = collectionManagementService.getCollectionManagementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CollectionManagementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出藏品管理 Excel")
    @PreAuthorize("@ss.hasPermission('museum:collection-management:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectionManagementExcel(@Valid CollectionManagementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CollectionManagementDO> list = collectionManagementService.getCollectionManagementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "藏品管理.xls", "数据", CollectionManagementRespVO.class,
                        BeanUtils.toBean(list, CollectionManagementRespVO.class));
    }



    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    @Parameters({
            @Parameter(name = "importType", description = "导入类型", example = "1:藏品导入，2:二维采集，3:三维采集")
    })
    public void importTemplate(HttpServletResponse response,
                               @RequestParam(value = "importType", required = false) Integer importType) throws IOException {
        switch (importType){
            case 1:
                List<CollectionImportExcelVO> clist = Arrays.asList(
                        CollectionImportExcelVO.builder().name("样例数据").build(),
                        CollectionImportExcelVO.builder().name("样例数据").build()
                );
                ExcelUtils.write(response, "藏品导入模版.xls", "藏品列表", CollectionImportExcelVO.class, clist);
                break;
            case 2:
                List<CollectionImageImportExcelVO> ilist = Arrays.asList(
                        CollectionImageImportExcelVO.builder().imageFilename("样例数据").build(),
                        CollectionImageImportExcelVO.builder().imageFilename("样例数据").build()
                );
                ExcelUtils.write(response, "二维采集导入模板.xls", "二维采集列表", CollectionImageImportExcelVO.class, ilist);
                break;
            case 3:
                List<Collection3dImportExcelVO> dlist = Arrays.asList(
                        Collection3dImportExcelVO.builder().resultName("样例数据").build(),
                        Collection3dImportExcelVO.builder().resultName("样例数据").build()
                );
                ExcelUtils.write(response, "三维采集导入模板.xls", "三维采集列表", Collection3dImportExcelVO.class, dlist);
                break;
            default:
                throw exception(GlobalErrorCodeConstants.ERROR_CONFIGURATION);
        }
    }

    @PostMapping("/import")
    @Operation(summary = "导入数据")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true"),
            @Parameter(name = "importType", description = "导入类型", example = "1:藏品导入，2:二维采集，3:三维采集")
    })
    @PreAuthorize("@ss.hasPermission('museum:collection-management:import')")
    public CommonResult<CollectionImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                            @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
                                                            @RequestParam(value = "importType", required = false) Integer importType)throws Exception {
        return success(collectionManagementService.importDataList(file, updateSupport,importType));
    }

}